package com.elink.enginerring.subject;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elink.enginerring.common.BaseCo;
import com.elink.enginerring.datasource.DataSourceContextHolder;
import com.elink.enginerring.datasource.DataSourceType;
import com.elink.enginerring.subject.entity.Catalog;
import com.elink.enginerring.subject.entity.Subject;
import com.elink.enginerring.subject.service.ISubjectService;

@Controller
@RequestMapping("/view/subject")
@Api(value="专题的接口")
public class SubjectController {
	@Autowired
	private ISubjectService subjectService;
	
	/**
	 * 获取专题列表
	 * @param co
	 * @return
	 */
	@RequestMapping(value="/getList",method=RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="分页获取专题",httpMethod="GET")
	public Map<String, Object>getList(@ModelAttribute @Valid BaseCo co){
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return subjectService.getList(co);
	}

	/**
	 * 添加专题
	 * @param subject
	 * @return
	 */
	@RequestMapping(value="/addSubject",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="添加专题",httpMethod="POST")
	public Map<String, Object> addSubject(@ModelAttribute @Valid Subject subject){
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return  subjectService.addSubject(subject);
	}
	
	/**
	 * 批量删除专题
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="/del",method=RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="批量删除专题",httpMethod="GET")
	public Map<String, Object> del(String  ids){
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return null;
//		return  subjectService.delSubject(ids);
	}
	
	/**
	 * 根据专题ID获取目录列表
	 * @param subjectId
	 * @return
	 */
	@RequestMapping(value="/getCatalogListBySubjectId",method=RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="根据专题ID获取目录列表",httpMethod="GET")
	public Map<String , Object>getCatalogListBySubjectId(@ModelAttribute @Valid BaseCo co,String subjectId){
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return  subjectService.getCatalogListBySubjectId(subjectId,co);
	}

	/**
	 * 添加目录
	 * @param cl
	 * @param subjectId
	 * @return
	 */
	@RequestMapping(value="/addCatalogBySubjectId",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="添加目录",httpMethod="POST")
	public Map<String , Object>addCatalogBySubjectId(@ModelAttribute @Valid Catalog cl, String subjectId){
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		cl.setSubjectId(subjectId);
		return  subjectService.addCatalogBySubjectId(cl);
	}
	
	/**
	 * 根据目录ID获取引用列表
	 * @param co
	 * @param subjectId
	 * @param type 0--资料 1--博文 2--课程 3--新闻 4--百问百答
	 * @return
	 */
	@RequestMapping(value = "/getSubjectFileByCatalogId", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="根据目录ID获取引用列表",httpMethod="GET")
	public Map<String, Object> getSubjectFileByCatalogId(@ModelAttribute @Valid BaseCo co,String catalogId,int type) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return subjectService.getSubjectFileByCatalogId(catalogId,type,co);
	}
	
	/**
	 * 根据目录ID添加引用列表
	 * @param co
	 * @param subjectId
	 * @param type 0--资料 1--博文 2--课程 3--新闻 4--百问百答
	 * @return
	 */
	@RequestMapping(value = "/addSubjectFileByCatalogId", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="根据目录ID获取引用列表",httpMethod="GET")
	public Map<String, Object> addSubjectFileByCatalogId(@ModelAttribute @Valid BaseCo co,String catalogId,int type) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return subjectService.addSubjectFileByCatalogId(catalogId,type,co);
	}
	/**
	 * 引用资料
	 * @param catalogId
	 * @param dcIds
	 * @return
	 */
	@RequestMapping(value = "/addFile", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addFile(String catalogId,
			@RequestParam(value="dcIds",required=true)String dcIds) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return subjectService.addFile(catalogId,dcIds);
	}
	
	/**
	 * 引用博文
	 * @param catalogId
	 * @param dcIds
	 * @return
	 */
	@RequestMapping(value = "/addBlog", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addBlog(String catalogId,
			@RequestParam(value="dcIds",required=true)String dcIds) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return subjectService.addBlog(catalogId,dcIds);
	}
	
	/**
	 * 引用课程
	 * @param catalogId
	 * @param dcIds
	 * @return
	 */
	@RequestMapping(value = "/addCourse", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addCourse(String catalogId,
			@RequestParam(value="dcIds",required=true)String dcIds) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return subjectService.addCourse(catalogId,dcIds);
	}
	
	/**
	 * 引用新闻
	 * @param catalogId
	 * @param dcIds
	 * @return
	 */
	@RequestMapping(value = "/addNews", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addNews(String catalogId,
			@RequestParam(value="dcIds",required=true)String dcIds) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return subjectService.addNews(catalogId,dcIds);
	}
	
	/**
	 * 引用问答
	 * @param catalogId
	 * @param dcIds
	 * @return
	 */
	@RequestMapping(value = "/addPostbar", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addPostbar(String catalogId,
			@RequestParam(value="dcIds",required=true)String dcIds) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return subjectService.addPostbar(catalogId,dcIds);
	}
}
